package org.luo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.luo.mapper.UserMapper;
import org.luo.pojo.Result;
import org.luo.pojo.Users;
import org.luo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public Result register(Users users) {
        //去除前后可能存在的空格
        users.setUsername(users.getUsername().trim());
        users.setPassword(users.getPassword().trim());
        //判断用户明密码不能为空
        if (StringUtils.isBlank(users.getUsername()) || StringUtils.isBlank(users.getPassword())) {
            return Result.error("账号密码不能为空");
        }
        //查询用户是否存在
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("username",users.getUsername());
        Users selectOne = userMapper.selectOne(wrapper);
        if (selectOne != null){
            return Result.error( users.getUsername() +  "已存在！");
        }
       userMapper.insert(users);
        return Result.success(null);


    }
}
